<?php

namespace App\Http\Controllers;

use App\Models\Pay;
use App\Models\User;
use Illuminate\Http\Request;
use DB;
use Mail;

class CustomerController extends AdminController
{
   public function  index(Request $request){

       $where=array();
       if(!empty($request->name)){
           $where[]=array(DB::raw("concat(firstname,lastname)"),'like',"%".$request->name."%");
       }
       if(!empty($request->email)){
           $where[]=array("users.email",'like',"%".$request->email."%");
       }
       if(!empty($request->phone)){
           $where[]=array("users.phone",'like',"%".$request->phone."%");
       }


      $list = User::where($where)->orderby("active",'ASC')->orderby("id","desc")->paginate(20);

       $append = array();
       $re = $request->toArray();
       foreach ($re as $key => $value) {
           $append[$key] = $value;
       }
       $list->appends($append);

       return view("customer.list",['list'=>$list]);

   }

   public function active($id){

       $info = User::find($id);

       $info->active='1';
       if($info->save()){

           $email = $info->email;

           /**
            * 邮件通知 登录地址和用户名密码
            */
           Mail::raw("尊敬的用户，恭喜你在福瑞斯注册的账号已经激活成功，请登录福瑞斯官网查看。",function ($message) use($email){
               $message->subject("账号激活成功");
               $message->to($email);
           });

           $this->setlog(json_encode($info),'帮用户 "'.$info->firstname.$info->lastname.'" 激活福瑞斯账户');

           return redirect('/customer')->with("messages",["0"=>'激活成功']);

       }else{
           return back()
               ->withErrors(array("激活失败"))
               ->withInput();
       }



   }

   public function settran(Request $request){

       $info = User::where(["id"=>$request->id,'tranid'=>$request->val])->count();
       if($info){
           echo '2';
       }else{
          $user =  User::where(["id"=>$request->id])->first();
           $user->tranid = $request->val;
           if($user->save()){
               $this->setlog(json_encode($user),'给用户 "'.$user->firstname.$user->lastname.'" 设置交易账号');
                echo 1;
           }else{
                echo 3;
           }
       }

   }

}
